﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Infrastructure.Crosscutting.Comm.Service
{
    public class MvcResultService
    {
        static readonly List<string> mvcActionResultTypeList = new List<string>()
        {
            "ActionResult","ContentResult","EmptyResult","HttpUnauthorizedResult",
            "JavaScriptResult","JsonResult","FileResult","FileContentResult",
            "FilePathResult","FileStreamResult","RedirectResult","RedirectToRouteResult",
            "ViewResultBase","ViewResult","PartialViewResult"
        };

        static readonly List<string> jsonResultList = new List<string>()
        {
            "JavaScriptResult","JsonResult"
        };

        static readonly List<string> fileResultList = new List<string>()
        {
            "FilePathResult","FileStreamResult","ContentResult","FileResult","FileContentResult"
        };

        static readonly List<string> viewResultList = new List<string>()
        {
            "RedirectResult","ViewResultBase","ViewResult","PartialViewResult","ActionResult"
        };

        static readonly List<string> otherResultList = new List<string>()
        {
            "EmptyResult","HttpUnauthorizedResult","RedirectToRouteResult"
        };

        public static bool IsJsonResult(MethodInfo method)
        {
            var res = false;
            res = Infrastructure.Crosscutting.Comm.Service.MethodInfoService.MethodReturnTypeIsTargetType(method, jsonResultList);
            return res;
        }

        public static bool IsOtherResult(MethodInfo method)
        {
            var res = false;
            res = Infrastructure.Crosscutting.Comm.Service.MethodInfoService.MethodReturnTypeIsTargetType(method, otherResultList);
            return res;
        }

        public static bool IsFileResult(MethodInfo method)
        {
            var res = false;
            res =Infrastructure.Crosscutting.Comm.Service.MethodInfoService. MethodReturnTypeIsTargetType(method, fileResultList);
            return res;
        }

        public static bool IsViewResult(MethodInfo method)
        {
            var res = false;
            res = Infrastructure.Crosscutting.Comm.Service.MethodInfoService.MethodReturnTypeIsTargetType(method, viewResultList);
            return res;
        }

        public static bool IsMvcActionResult(Type target)
        {
            var res = false;
            if (mvcActionResultTypeList.Contains(target.Name))
            {
                res = true;
            }
            else
            {
                Type temp = target.BaseType;
                do
                {
                    var copy = temp;
                    if (temp != null)
                    {
                        if (mvcActionResultTypeList.Contains(temp.Name))
                        {
                            res = true; break;
                        }
                        temp = copy.BaseType;
                    }
                } while (temp != null);
            }
            return res;
        }
    }
}
